//
//  CityViewController.m
//  GOGOTravel
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "CityViewController.h"
#import "hottest_placesModle.h"
#import "PictureViewController.h"
#import "HongguoCollectionViewCell.h"
#import "FuJin.h"
#import "FuJinErViewController.h"
#import "ExperienceDetailsViewController.h"
#import "GiFHUD.h"
const CGFloat BackGroupHeight = 200;  //全局变量

@interface CityViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) NSDictionary *dataDic;
@property (nonatomic, retain) NSMutableArray *hottest_placesModleArr;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIImageView *imageBig;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) BaseCollectionView *collection;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, retain) NSMutableArray *TuiJianArrData;/**<  推荐数据源 */

@end

@implementation CityViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)dealloc{
    [_dataArr release];
    [_hottest_placesModleArr release];
    [_tableView release];
    [_imageBig release];
    [_titleLabel release];
    [_button release];
    [_collection release];
    [_url release];
    [_url2 release];
    [_images release];
    [_name release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    //初始化字典
    self.dataDic = [NSDictionary dictionary];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-jiantouzuo"] style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    
    self.navigationItem.leftBarButtonItem = item;
    [item release];
    
    
    [self handleData];
    
    [self createTableView];
    
    [self createForeButton];

    [self handleData2];
    
    [GiFHUD setGifWithImageName:@"b879f2e742d94b2a8f2362d0ca7e18c7.gif"];
    [GiFHUD show];

}

- (void)setName:(NSString *)name{
    if (_name != name) {
        [_name release];
        _name = [name retain];
    }
    
    self.navigationItem.title = _name;
}

- (void)itemAction:(UIBarButtonItem *)item{
[self.navigationController popViewControllerAnimated:YES];
    
}
- (void)createTableView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing =  10;
    layout.minimumLineSpacing = 10;
    
    layout.sectionInset = UIEdgeInsetsMake(100, 10, 10, 10);
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 30)/2, 200);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    
    self.collection = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 60, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height+40 ) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    
    
    [self.view addSubview:_collection];
    _collection.contentInset = UIEdgeInsetsMake(BackGroupHeight, 0, 0, 0);

    
    
    
    
    [_collection registerClass:[HongguoCollectionViewCell class] forCellWithReuseIdentifier:@"HongguoCollectionViewCell"];
    

}

-(void)setType:(NSString *)type{
    if (_type != type) {
        [_type release];
        _type = [type retain];
    }
}

- (void)setSpot_id:(NSString *)spot_id{
    if (_spot_id != spot_id) {
        [_spot_id release];
        _spot_id = [spot_id retain];
    }
}

/**<  不可错过点击方法 */
- (void)buttonNoAction:(UIButton *)button{
    NSString *str = @"http://web.breadtrip.com/mobile/destination/3/9990/intro/";
    NSString *str1 = [str stringByReplacingOccurrencesOfString:@"3" withString:_type];
    NSString *result = [str1 stringByReplacingOccurrencesOfString:@"9990" withString:_spot_id];
    
    ExperienceDetailsViewController *ec = [[ExperienceDetailsViewController alloc] init];
    ec.url = result;
    [self.navigationController pushViewController:ec animated:YES];
    [ec release];
    
}

/** 主题榜单的点击方法 */
- (void)buttonNo1Action:(UIButton *)button{
    NSString *str = @"http://web.breadtrip.com/mobile/destination/3/9990/top10_list/";
    NSString *str1 = [str stringByReplacingOccurrencesOfString:@"3" withString:_type];
    NSString *result = [str1 stringByReplacingOccurrencesOfString:@"9990" withString:_spot_id];
    
    ExperienceDetailsViewController *ec = [[ExperienceDetailsViewController alloc] init];
    ec.url = result;
    [self.navigationController pushViewController:ec animated:YES];
    [ec release];
    
}
/** 推荐路线 */
- (void)buttonNo2Action:(UIButton *)button{
    NSString *str = @"http://web.breadtrip.com/mobile/destination/3/9990/routes/";
    NSString *str1 = [str stringByReplacingOccurrencesOfString:@"3" withString:_type];
    NSString *result = [str1 stringByReplacingOccurrencesOfString:@"9990" withString:_spot_id];
    
    ExperienceDetailsViewController *ec = [[ExperienceDetailsViewController alloc] init];
    ec.url = result;
    [self.navigationController pushViewController:ec animated:YES];
    [ec release];
    
}

/** 实用须知 */
- (void)buttonNo3Action:(UIButton *)button{
    NSString *str = @"http://web.breadtrip.com/mobile/destination/3/9990/overview/";
    
    NSString *str1 = [str stringByReplacingOccurrencesOfString:@"3" withString:_type];
    NSString *result = [str1 stringByReplacingOccurrencesOfString:@"9990" withString:_spot_id];
    
    ExperienceDetailsViewController *ec = [[ExperienceDetailsViewController alloc] init];
    ec.url = result;
    [self.navigationController pushViewController:ec animated:YES];
    [ec release];
    
}

- (void)createForeButton{
    
    UIButton *buttonNo = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonNo.frame = CGRectMake(30, 0, 40, 40);
    buttonNo.backgroundColor = [UIColor clearColor];
    [buttonNo setImage:[UIImage imageNamed:@"iconfont-smile-2.png"]
              forState:UIControlStateNormal];
    
    [buttonNo addTarget:self action:@selector(buttonNoAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:buttonNo];
    
    UILabel *labelNo = [[UILabel alloc] init];
    labelNo.frame = CGRectMake(26, 37, 70, 20);
    labelNo.backgroundColor = [UIColor clearColor];
    labelNo.text = @"不可错过";
    labelNo.font = [UIFont systemFontOfSize:14];
    labelNo.textColor = [UIColor blackColor];
    [self.view addSubview:labelNo];
    [labelNo release];
    
    UIButton *buttonNo1 = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonNo1.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 70, 0, 40, 40);
    buttonNo1.backgroundColor = [UIColor clearColor];
    [buttonNo1 setImage:[UIImage imageNamed:@"iconfont-theme.png"] forState:UIControlStateNormal];
    [buttonNo1 addTarget:self action:@selector(buttonNo1Action:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonNo1];
    
    UILabel *labelNo1 = [[UILabel alloc] init];
    labelNo1.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 75, 37, 70, 20);
    labelNo1.backgroundColor = [UIColor clearColor];
    labelNo1.text = @"主题榜单";
    labelNo1.font = [UIFont systemFontOfSize:14];
    labelNo1.textColor = [UIColor blackColor];
    [self.view addSubview:labelNo1];
    [labelNo1 release];
    
    
    
    UIButton *buttonNo2 = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonNo2.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2 + 10 , 0, 40, 40);
    buttonNo2.backgroundColor = [UIColor clearColor];
    [buttonNo2 setImage:[UIImage imageNamed:@"iconfont-luxian.png"] forState:UIControlStateNormal];
    [buttonNo2 addTarget:self action:@selector(buttonNo2Action:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:buttonNo2];
    
    UILabel *labelNo2 = [[UILabel alloc] init];
    labelNo2.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2+8 , 37, 70, 20);
    labelNo2.backgroundColor = [UIColor clearColor];
    labelNo2.text = @"推荐路线";
    labelNo2.font = [UIFont systemFontOfSize:14];
    labelNo2.textColor = [UIColor blackColor];
    [self.view addSubview:labelNo2];
    [labelNo2 release];
    
    UIButton *buttonNo3 = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonNo3.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 80 , 0, 40, 40);
    buttonNo3.backgroundColor = [UIColor clearColor];
    [buttonNo3 setImage:[UIImage imageNamed:@"iconfont-notice.png"] forState:UIControlStateNormal];
    [buttonNo3 addTarget:self action:@selector(buttonNo3Action:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonNo3];
    
    UILabel *labelNo3 = [[UILabel alloc] init];
    labelNo3.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 80, 37,70, 20);
    labelNo3.backgroundColor = [UIColor clearColor];
    labelNo3.text = @"实用须知";
    labelNo3.font = [UIFont systemFontOfSize:14];
    labelNo3.textColor = [UIColor blackColor];
    [self.view addSubview:labelNo3];
    [labelNo3 release];

    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 200)/2, 20, 200, 50);
    label.text = @"推荐路线";
    label.font = [UIFont systemFontOfSize:19];
    label.textAlignment = NSTextAlignmentCenter;
    [_collection addSubview:label];
    [label release];
    
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 1) {
        return 0;
    }else{
        return _TuiJianArrData.count;
        
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    FuJin *model = _TuiJianArrData[indexPath.row];
    
    FuJinErViewController *fuJin = [[FuJinErViewController alloc] init];
    fuJin.model = model;
    [self.navigationController pushViewController:fuJin animated:YES];
    
    [fuJin release];
    
    
    
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HongguoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HongguoCollectionViewCell" forIndexPath:indexPath];
    
    FuJin *model = _TuiJianArrData[indexPath.row];
    [cell.imageView1 sd_setImageWithURL:[NSURL URLWithString:model.cover_route_map_cover] placeholderImage:[UIImage imageNamed:PLACE_HLODER]];
   
    cell.labelName.text = model.name;
    cell.labelNum.text = [NSString stringWithFormat:@"%@ 人喜欢", model.tips_count];
    cell.labelNum.textColor = [UIColor whiteColor];
    
    return cell;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    
    CGFloat yoffset = scrollView.contentOffset.y;
    //    CGFloat xoffset = (yoffset + BackGroupHeight) * 2;
    CGFloat xoffset = scrollView.contentOffset.x;
    if (yoffset < -BackGroupHeight) {
        CGRect rect = _imageBig.frame;
        rect.origin.y = yoffset;
        rect.size.height = -yoffset;
        rect.origin.x = xoffset;
        rect.size.width = self.view.frame.size.width +fabs(xoffset) * 2;
        _imageBig.frame = rect;
    }
    CGFloat alpha = (yoffset +BackGroupHeight) / BackGroupHeight;
//    
//    [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[UIColorFromRGB(0x099cc) colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];
    
    _titleLabel.alpha = alpha;
    //跳转到图片VC
    if (yoffset < -115-BackGroupHeight) {

        
        PictureViewController *PC = [[PictureViewController alloc] init];
        PC.urlFirst = _url;
        
       
        
        PC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:PC animated:YES completion:^{
            
        }];
        
        [PC release];
    }

}

- (void)handleData2 {

    [AFNetWorkingHandle GETRequestWithURLString:_url2 completionWithSuccess:^(id result) {
        
        NSArray *arr = result[@"items"];
        
        self.TuiJianArrData = [FuJin ModelHandleWithArray:arr];
        
        
        
        [_collection reloadData];
        
        
        [GiFHUD dismiss];
    } withFailure:^(id error) {
        
    }];
    
}

- (void)handleData{
   
    [AFNetWorkingHandle GETRequestWithURLString:_url completionWithSuccess:^(id result) {
        
        
        _dataDic = result;
        NSArray *arr = _dataDic[@"hottest_places"];
        NSDictionary *firstDic  = [arr firstObject];
        self.images = firstDic[@"photo"];
        
        
        self.dataArr = [hottest_placesModle ModelHandleWithArray:arr];
        
       
        
        
        self.hottest_placesModleArr = [hottest_placesModle ModelHandleWithArray:arr];
       
        [_tableView reloadData];
        
        /**<  设置大图片 */
        self.imageBig = [[UIImageView alloc] initWithFrame:CGRectMake(0, -BackGroupHeight, self.view.frame.size.width, BackGroupHeight)];
        [_imageBig sd_setImageWithURL:[NSURL URLWithString:_images] placeholderImage:[UIImage imageNamed:PLACE_HLODER]];
        [_collection addSubview:_imageBig];
        [_imageBig release];
        
        
        
        [_collection reloadData];
        
    } withFailure:^(id error) {
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
